package com.neusoft.databus.policy.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.neusoft.bizcore.web.dto.result.PageResultDTO;
import com.neusoft.bizcore.web.dto.result.ResultDTO;
import com.neusoft.bizcore.web.dto.result.ResultListDTO;
import com.neusoft.bizcore.web.exception.CustomRuntimeException;
import com.neusoft.bizcore.web.support.Searchable;
import com.neusoft.databus.common.constant.DatabusErrorCode;
import com.neusoft.databus.policy.converter.EventConverter;
import com.neusoft.databus.policy.dto.EventDTO;
import com.neusoft.databus.policy.enums.EventStatus;
import com.neusoft.databus.policy.model.Event;
import com.neusoft.databus.policy.repository.EventRepository;
import com.neusoft.databus.policy.service.EventService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 策略事件管理控制器
 *
 * @author naxiang
 */
@Api("策略事件管理控制器")
@RestController
@RequestMapping(value = "/api/w/events")
public class EventController {
    @Autowired
    private EventRepository eventRepository;
    @Autowired
    private EventService eventService;
    @Autowired
    private EventConverter eventConverter;

    @ApiOperation("查询所有事件")
    @GetMapping
    public ResultListDTO<EventDTO> index(final Searchable searchable) {
        final List<Event> events = this.eventRepository.search(searchable);
        final ResultListDTO<EventDTO> result = ResultListDTO.success(this.eventConverter.toListDTO(events));
        return result;
    }

    @ApiOperation("分页查询所有事件")
    @GetMapping("/p")
    public PageResultDTO<EventDTO> search(final Searchable searchable, final Pageable pageable) {
        final Page<Event> models = this.eventRepository.search(pageable, searchable);
        return PageResultDTO.success(this.eventConverter.toPageDTO(models));
    }

    /**
     * 解除告警事件
     *
     * @param code 解除告警事件
     * @return ResultDTO<Void>
     * @throws CustomRuntimeException
     */
    @ApiOperation("解除告警事件")
    @PutMapping("/{code}/relase")
    public ResultDTO<Void> relase(@PathVariable final String code) {
        final List<Event> events = this.eventRepository.findByCodeAndStatus(code, EventStatus.UNFIXED);
        if ((null == events) || (events.size() == 0)) {
            throw new CustomRuntimeException(DatabusErrorCode.ERR_CODE_404001, new Object[] {code });
        }
        this.eventService.eventFixed(events);
        return ResultDTO.success();
    }

    /**
     * 批量解除告警事件
     *
     * @param code 批量解除事件
     * @return ResultDTO<Void>
     * @throws CustomRuntimeException
     */
    @ApiOperation("批量解除告警事件")
    @PutMapping("/relase")
    public ResultDTO<Void> batchRelase(@RequestBody final List<String> codes) {
        final Searchable searchable = new Searchable();
        final String[] eventCodes = {};
        searchable.put("code", codes.toArray(eventCodes));
        searchable.put("status", EventStatus.UNFIXED);
        final List<Event> events = this.eventRepository.search(searchable);
        this.eventService.eventFixed(events);
        return ResultDTO.success();
    }
}
